package com.aphrodite.io.sip.dialog;

import com.aphrodite.io.sip.message.Message;
import com.aphrodite.io.sip.message.SipMethods;
import com.aphrodite.io.sip.provider.MethodIdentifier;
import com.aphrodite.io.sip.provider.SipProvider;
import com.aphrodite.io.sip.provider.SipProviderListener;

/**
 * InviteDialogAccepter listens for new incoming invite requests that create an
 * InviteDialog.
 */
public class InviteDialogAccepter implements SipProviderListener {
	/** InviteDialogAccepterListener that captures new invite dialogs. */
	InviteDialogAccepterListener listener;

	/** SipProvider that receives incoming invite requests. */
	SipProvider sip_provider;

	/**
	 * Creates a new InviteDialogAccepter of type <i>method</i>, and starts
	 * listening for incoming invite requests.
	 */
	public InviteDialogAccepter(SipProvider sip_provider, InviteDialogAccepterListener listener) {
		this.listener = listener;
		sip_provider.addSipProviderListener(new MethodIdentifier(SipMethods.INVITE), this);
	}

	/** Stops listening for incoming invite requests. */
	public void halt() {
		sip_provider.removeSipProviderListener(new MethodIdentifier(SipMethods.INVITE));
	}

	/** Method derived from interface SipListener. */
	public void onReceivedMessage(SipProvider provider, Message msg) {
		if (listener != null)
			listener.onNewInviteDialog(this, new InviteDialog(sip_provider, msg, listener), msg.getToHeader()
					.getNameAddress(), msg.getFromHeader().getNameAddress(), msg.getBody(), msg);
	}

}
